﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.WebAPI.Models;

namespace ThresholdAssistant.WebAPI.Repository
{

    /// <summary>
    /// 会员表仓储
    /// </summary>
    public class MemberRepository : BaseRepository, IBaseAutofac
    {
        /// <summary>
        /// 新增
        /// </summary>
        public async Task<long> AddAsync(Member model)
        {
            string insertSql = @"insert into `t_member`
                                    (`wx_open_id`,`nick_name`,`sex`,`profile`,`phone`,`group_id`,`is_master`,`create_time`,`update_time`,`remark`)
                                values
                                    (@WxOpenId,@NickName,@Sex,@Profile,@Phone,@GroupId,@IsMaster,@CreateTime,@UpdateTime,@Remark);
                                SELECT @@IDENTITY;";
            return await mydb.ExecuteScalarAsync<long>(insertSql, model);
        }

        /// <summary>
        /// 查询
        /// </summary>
        public async Task<int> GetMemberCountByGroupIdAsync(long groupId)
        {
            string querySql = @"select
                                count(1)
                             from `t_member`
                             where group_id = @GroupId;";
            return await mydb.GetFirstOrDefaultAsync<int>(querySql, new { GroupId = groupId });
        }

        /// <summary>
        /// 查询
        /// </summary>
        public async Task<Member> GetInfoAsync(long id)
        {
            string querySql = @"select
                                `id`, `wx_open_id`, `nick_name`, `sex`, `profile`, `phone`, `group_id`, `is_master`, `create_time`, `update_time`, `remark`
                             from `t_member`
                             where id = @Id;";
            return await mydb.GetFirstOrDefaultAsync<Member>(querySql, new { Id = id });
        }

        /// <summary>
        /// 查询
        /// </summary>
        public async Task<Member> GetInfoByInviteCodeAsync(string inviteCode)
        {
            string querySql = @"select
                                `id`, `wx_open_id`, `nick_name`, `sex`, `profile`, `phone`, `group_id`, `is_master`, `create_time`, `update_time`, `remark`
                             from `t_member`
                             where wx_open_id like CONCAT('%', @InviteCode, '');";
            return await mydb.GetFirstOrDefaultAsync<Member>(querySql, new { InviteCode = inviteCode });
        }

        /// <summary>
        /// 查询
        /// </summary>
        public string GetOpenId(long id)
        {
            string querySql = @"select
                                 `wx_open_id`
                             from `t_member`
                             where id = @Id;";
            return mydb.GetFirstOrDefault<string>(querySql, new { Id = id });
        }

        /// <summary>
        /// 根据openid查询会员信息
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public async Task<Member> GetInfoByOpenIdAsync(string openId)
        {
            string querySql = @"select
                                `id`, `wx_open_id`, `nick_name`, `sex`, `profile`, `phone`, `group_id`, `is_master`, `create_time`, `update_time`, `remark`
                             from `t_member`
                             where wx_open_id = @OpenId;";
            return await mydb.GetFirstOrDefaultAsync<Member>(querySql, new { OpenId = openId });
        }

        /// <summary>
        /// 修改
        /// </summary>
        public async Task<bool> UpdateAsync(Member model)
        {
            string updateSql = @"update
                                    `t_member`
                                set
                                    `wx_open_id`=@WxOpenId,
                                    `nick_name`=@NickName,
                                    `sex`=@Sex,
                                    `profile`=@Profile,
                                    `phone`=@Phone,
                                    `group_id`=@GroupId,
                                    `is_master`=@IsMaster,
                                    `update_time`=@UpdateTime,
                                    `remark`=@Remark
                                where `id` = @Id";
            return await mydb.ExecuteAsync(updateSql, model);
        }

        /// <summary>
        /// 修改微信相关信息
        /// </summary>
        public async Task<bool> UpdateWxInfoAsync(Member model)
        {
            string updateSql = @"update
                                    `t_member`
                                set
                                    `nick_name`=@NickName,
                                    `phone`=@Phone,
                                    `update_time`=@UpdateTime
                                where `id` = @Id";
            return await mydb.ExecuteAsync(updateSql, model);
        }

        /// <summary>
        /// 修改头像
        /// </summary>
        public async Task<bool> UpdateProfileAsync(long id, string profile)
        {
            string updateSql = @"update
                                    `t_member`
                                set
                                    `profile`=@Profile,
                                    `update_time`=@UpdateTime
                                where `id` = @Id";
            return await mydb.ExecuteAsync(updateSql, new { Id = id, UpdateTime = DateTime.Now, Profile = profile });
        }

        /// <summary>
        /// 修改微信相关信息
        /// </summary>
        public async Task<bool> UpdatePhoneAsync(long id, string phone)
        {
            string updateSql = @"update
                                    `t_member`
                                set
                                    `phone`=@Phone,
                                    `update_time`=@UpdateTime
                                where `id` = @Id";
            return await mydb.ExecuteAsync(updateSql, new { Phone = phone, Id = id, UpdateTime = DateTime.Now });
        }

        /// <summary>
        /// 修改
        /// </summary>
        public async Task<bool> UpdateIsMasterAsync(long memberId, int isMaster)
        {
            string updateSql = @"update
                                    `t_member`
                                set
                                    `is_master`=@IsMaster,
                                    `update_time`=@UpdateTime
                                where `id` = @MemberId";
            return await mydb.ExecuteAsync(updateSql, new { MemberId = memberId, IsMaster = isMaster,UpdateTime = DateTime.Now});
        }

        /// <summary>
        /// 修改
        /// </summary>
        public async Task<bool> UpdateGroupIdAsync(IEnumerable<long> memberIdList, long grupId)
        {
            string updateSql = @"update
                                    `t_member`
                                set
                                    `group_id`=@GroupId,
                                    `update_time`=@UpdateTime
                                where `id` in @MemberIdList";
            return await mydb.ExecuteAsync(updateSql, new { MemberIdList = memberIdList, GroupId = grupId, UpdateTime = DateTime.Now });
        }

        /// <summary>
        /// 修改
        /// </summary>
        public async Task<IEnumerable<long>> GetMemberIdListByGroupIdAsync(long grupId)
        {
            string updateSql = @"select `id` from `t_member` where `group_id`=@GroupId,";
            return await mydb.GetListAsync<long>(updateSql, new { GroupId = grupId });
        }

        /// <summary>
        /// 获取群组成员列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Member>> GetMemberListByGroupIdAsync(long groupId)
        {
            string querySql = @"select
                                `id`, `wx_open_id`, `nick_name`, `sex`, `profile`, `phone`, `group_id`, `is_master`, `create_time`, `update_time`, `remark`
                             from `t_member`
                             where group_id = @GroupId;";
            return await mydb.GetListAsync<Member>(querySql, new { GroupId = groupId });
        }

        /// <summary>
        /// 获取群主列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Member>> GetMemberMasterList()
        {
            string querySql = @"select
                                `id`, `wx_open_id`, `nick_name`, `sex`, `profile`, `phone`, `group_id`, `is_master`, `create_time`, `update_time`, `remark`
                             from `t_member`
                             where is_master = 1;";
            return await mydb.GetListAsync<Member>(querySql, new {  });
        }
    }
}